* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* Treatment Effects over Time for Vaccine 5                               *
* **************************************************************************** *
/*
** Purpose:  Generates Figure :
								Treatment Effects over Time for Vaccine 5

** Written by:
                Anne Karing    (akaring@uchiago.edu)
                Jonas Guthoff  (jonas.guthoff@gmail.com)

*/
* **************************************************************************** *
* **************************************************************************** *

	* Set options for figures
	global graph  "graphregion(color(white)) bgcolor(white) legend(region(lc(none))) title(, color(black) span)"

	cd   "${Replicate_SocialSignals_Out}/colorschemes/"

	grstyle init followup,  replace
	grstyle set plain, horizontal
	grstyle set compact
	grstyle set legend, nobox


* **************************************************************************** *
* Measles 1
* **************************************************************************** *


	use   "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_TreatmentEffectsOverTime.dta", clear


	* Compute the Months relative to the launch:
	format dob start %td

	gen    reference_days   = dob - start
	gen    reference_months = round((dob - start)/(365/12))

	tab    reference_months


	preserve
	gen     birthcohort = 1

	collapse (sum) birthcohort, by(reference_months)

	gen     timeVar  = _n
	egen    group    = seq(), block(2)
	replace group    = 7               if group > 7

	keep    reference_months timeVar group
	save "$Replicate_SocialSignals_dtaFinal/SICI_FollowUp_BirthCohort_Variable.dta", replace
	restore

	* Merge those back
	merge m:m reference_months using ///
	"$Replicate_SocialSignals_dtaFinal/SICI_FollowUp_BirthCohort_Variable.dta", keepusing(timeVar group) ///
	gen(MergeBirthCohort)


	summ    measles1, detail
	local   Observations = trim("`: display %-9.0gc r(N)'")

	tab     group     if measles1 !=.

	summ    group, detail
	local   group_min = `r(min)'
	local   group_max = `r(max)'

	local   Sample    =  ""
	local   BinsWave1 =  ""
	local   BinsWave2 =  ""

	* Store Cohort Sizes:
	foreach cohort of numlist `group_min'(1)`group_max' {
		summ  measles1                if group == `cohort'
		local N_`cohort' = trim("`: display %-9.0gc r(N)'")
		
		if   `cohort' == `group_min' {
			local Sample  = "`Sample' `N_`cohort''"
		}
		
		else  {
			local Sample  = "`Sample', `N_`cohort''"
		}
	}


	local ANC_dm      = " anc2 anc3 anc4  "
	local Controls_dm = " recall1 recall2 distance clinic_pop "

	summ  `ANC_dm' `Controls_dm'

	foreach group of numlist `group_min'(1)`group_max'  {
		areg    measles1 treat3 treat4 treat2      `Controls_dm'  `ANC_dm'   if group == `group' , absorb(strata) cluster(clinic)


		preserve
			matrix est       = r(table)
			matrix est_trans = est'
			clear
			svmat  est_trans, names(col)

			gen    group = `group'

			* keep only the treatment estimates:
			gen    count = _n
			keep if count[_n] < 4|count[_n]==_N

			summ   count, detail
			gen    const    = b              if count == `r(max)'
			egen   constant = min(const)
			drop   const

			recode count (1 = 3) (2 = 4) (3 = 2)

			lab def treat 1 "Signal at 4" 2 "Signal at 5" 3 "Uninformative" 9 "constant", modify
			lab val count treat

			gen   estimate = b
			gen   high_CI  = b + 1.96 * abs(se)
			gen   low_CI   = b - 1.96 * abs(se)

			tempfile birthgroup`group'
			save    `birthgroup`group''
		restore
	}


	* Append estimates to plot
	use    `birthgroup1', clear

	foreach group of numlist `group_min'(1)`group_max'{
		append  using `birthgroup`group''
	}

	* Add some spaces between:
	replace group = group - 0.15       if count == 2
	replace group = group + 0.15       if count == 4

	rename count     _deriv
	rename group     _at2
	rename estimate  _margin
	rename high_CI   _ci_ub
	rename low_CI    _ci_lb


	twoway  ///
	(scatter _margin _at2                 if _deriv == 2 , ///
	yscale(r(-0.15 0.2))  msymbol(d)  mcolor(blue)  lcolor(blue*0.7)   lpattern(solid) lwidth(medium) ylabel(-0.25(0.05)0.25))  ||  ///
	(rcapsym    _ci_lb _ci_ub  _at2       if _deriv == 2, color(blue*0.7) msymbol(none))           || ///
	(scatter _margin _at2                 if _deriv == 3 , ///
	yscale(r(-0.15 0.2))  msymbol(t)  mcolor(green) lcolor(green*0.7) lpattern(solid) lwidth(medium) ylabel(-0.25(0.05)0.25)) ||  ///
	(rcapsym    _ci_lb _ci_ub  _at2       if _deriv == 3, color(green*0.7) msymbol(none))          || ///
	(scatter _margin _at2                 if _deriv == 4,  ///
	yscale(r(-0.15 0.2))  msymbol(o)  mcolor(red)   lcolor(red*0.7)   lpattern(solid) lwidth(medium)  ylabel(-0.25(0.05)0.25)) ||  ///
	(rcapsym    _ci_lb _ci_ub  _at2       if _deriv == 4, color(red*0.7) msymbol(none)),  ///
	ytitle("Average Treatment Effect")                              ///
	xtitle("Birth Cohort in Months Relative To Launch")             ///
	ylabel(-0.2 -0.1 0 0.1 0.2)  ytick(-0.25(0.05)0.25)  ///
	yline(-0.25(0.05)0.25 , lwidth(thin) lcolor(gs15))  ///
	xlabel(1 `"0 to 1"' 2 "2 to 3" 3 "4 to 5" 4 "6 to 7" 5 "8 to 9" 6 "10 to 11" 7 `" "after" 12"')  ///
	legend(cols(3)                                                  ///
	order(1 "Uninformative Bracelet"  ///
	3 "Signal at 4"             ///
	5 "Signal at 5"))           ///
	note("N=`Observations'"           ///
	"Cohort sizes:`Sample'")
	graph export "${Replicate_SocialSignals_AFigures}/Figure_TreatmentEffectOverTime_Vaccine5_byCohorts.jpg", replace




* **************************************************************************** *
* End of the Dofile !!!
* **************************************************************************** *
